package com.yc.cloud.common.websocket.core;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.corundumstudio.socketio.SocketIOClient;
import com.yc.cloud.common.websocket.vo.PlayerVo;
import lombok.extern.slf4j.Slf4j;
import lombok.val;

import java.util.List;
import java.util.Map;

@Slf4j
public class SocketClientInfoManager {

    //通过将instance声明为volatile，可以确保在对象初始化完成之前，所有线程都能够正确地看到instance的状态
    private static volatile SocketClientInfoManager instance;

    private SocketClientInfoManager() {

    }

    public static SocketClientInfoManager getInstance() {
        if (instance == null) {
            synchronized (SocketClientInfoManager.class) {
                if (instance == null) {
                    instance = new SocketClientInfoManager();
                }
            }
        }
        return instance;
    }


    public String getUserNameFromDisplay(String display) {
        if (StrUtil.isNotEmpty(display) && display.contains(SocketConstants.SYMBOL)) {
            return display.split(SocketConstants.SYMBOL_ESCAPE)[0];
        }
        return display;
    }

    public String getTrueNameFromDisplay(String display) {
        if (StrUtil.isNotEmpty(display) && display.contains(SocketConstants.SYMBOL)) {
            val info = display.split(SocketConstants.SYMBOL_ESCAPE);
            if (info.length > 1) {
                log.debug("trueName=" + info[1]);
                return info[1];
            }
        }
        return "";
    }

    public String getPlatformFromDisplay(String display) {
        if (StrUtil.isNotEmpty(display) && display.contains(SocketConstants.SYMBOL)) {
            val info = display.split(SocketConstants.SYMBOL_ESCAPE);
            if (info.length > 2) {
                return info[2];
            }
        }
        return "";
    }

    public String getDisplayParamsByClient(SocketIOClient client) {
        // 获取客户端url参数（这里的userId是唯一标识）
        Map<String, List<String>> params = client.getHandshakeData().getUrlParams();
        List<String> userIdList = params.get(SocketConstants.URL_PARAM_DISPLAY);
        if (!CollUtil.isEmpty(userIdList)) {
            val display = userIdList.get(0);
            log.debug(display);
            return display;
        }
        return null;
    }


    public String getRoomParamsByClient(SocketIOClient client) {
        // 获取客户端url参数（这里的userId是唯一标识）
        Map<String, List<String>> params = client.getHandshakeData().getUrlParams();
        List<String> userIdList = params.get(SocketConstants.URL_PARAM_ROOM);
        if (!CollUtil.isEmpty(userIdList)) {
            return userIdList.get(0);
        }
        return null;
    }


    public String getSocketId(SocketIOClient client) {
        var id = client.getSessionId().toString();
        val socketIONamespace = client.getNamespace();
        if (socketIONamespace != null && StrUtil.isNotEmpty(socketIONamespace.getName())) {
            id = socketIONamespace.getName() + "#" + id;
        }
        return id;
    }

    public PlayerVo convertPlayerVo(SocketIOClient client) {
        val player = new PlayerVo();
        val display = getDisplayParamsByClient(client);
        player.setDisplay(display);
        player.setTrueName(getTrueNameFromDisplay(display));
        player.setUsername(getUserNameFromDisplay(display));
        player.setPlatform(getPlatformFromDisplay(display));
        player.setId(getSocketId(client));
//        player.setClient(client);
        return player;
    }

}
